package cn.zane.chatgpt.sdk.session.defaults;

import cn.zane.chatgpt.sdk.IOpenAiApi;
import cn.zane.chatgpt.sdk.common.Constants;
import cn.zane.chatgpt.sdk.domain.chat.ChatCompletionRequest;
import cn.zane.chatgpt.sdk.domain.chat.ChatCompletionResponse;
import cn.zane.chatgpt.sdk.domain.qa.QACompletionRequest;
import cn.zane.chatgpt.sdk.domain.qa.QACompletionResponse;
import cn.zane.chatgpt.sdk.session.OpenAiSession;
import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;

/**
 * @author: zane
 * @date: 2025-10-12
 * @description: OpenAiSession默认实现类
 */
@RequiredArgsConstructor
public class DefaultOpenAiSession implements OpenAiSession {

    private final IOpenAiApi openAiApi;

    @Override
    public QACompletionResponse completions(QACompletionRequest qaCompletionRequest) {
        return this.openAiApi.completions(qaCompletionRequest).blockingGet();
    }

    @Override
    public QACompletionResponse completions(String question) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("role", Constants.Role.USER.getCode());
        map.put("content", question);
        QACompletionRequest qaCompletionRequest = QACompletionRequest.builder().model(ChatCompletionRequest.Model.QWEN_PLUS.getCode()).messages(List.of(map)).build();
        System.out.println("请求内容=" + JSON.toJSONString(qaCompletionRequest));
        return this.openAiApi.completions(qaCompletionRequest).blockingGet();
    }

    @Override
    public ChatCompletionResponse completions(ChatCompletionRequest chatCompletionRequest) {
        return this.openAiApi.completions(chatCompletionRequest).blockingGet();
    }
}
